package pl.edu.agh.nn.data.provider;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import pl.edu.agh.nn.conf.InvalidNetworkConfigurationException;
import pl.edu.agh.nn.data.NetData;

public class FileInputProvider implements InputDataProvider {

	private List<NetData> inputData;
	private int dataPointer = 0;
	private String file;

	public FileInputProvider() {

	}

	public FileInputProvider(String file) {
		this.file = file;
	}

	@Override
	public boolean hasData() {
		return dataPointer < inputData.size();
	}

	@Override
	public double[] nextData() {
		return inputData.get(dataPointer++).getData();
	}

	@Override
	public void configure() throws InvalidNetworkConfigurationException {
		this.inputData = new ArrayList<NetData>();
		readDataFromFile();
	}

	@Override
	public void finish() {
		// do nothing
	}

	private void readDataFromFile() {
		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				String[] sValues = line.split(",");
				double data[] = new double[sValues.length];

				for (int i = 0; i < sValues.length; i++) {
					double dValue = Double.parseDouble(sValues[i].trim());
					data[i] = dValue;
				}
				inputData.add(new NetData(data));
			}
		} catch (Exception e) {
			System.err.println("Could not read input data");
			e.printStackTrace();
		}
	}

}
